package com.zp.business.impl.infusion;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.infusion.InfusionItem;
import com.zp.business.entity.infusion.bo.InfusionItemBo;
import com.zp.business.entity.infusion.vo.InfusionItemVo;
import com.zp.business.mapper.infusion.InfusionItemMapper;
import com.zp.business.service.infusion.IInfusionItemService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 输液室登记项目Service业务层处理
 *
 * @author zhang peng
 * @ date 2025-02-08
 */
@RequiredArgsConstructor
@Service
public class InfusionItemServiceImpl implements IInfusionItemService {

    private final InfusionItemMapper baseMapper;

    /**
     * 查询输液室登记项目
     */
    @Override
    public InfusionItemVo queryById(String infusionItemId) {
        return baseMapper.selectVoById(infusionItemId);
    }

    /**
     * 查询输液室登记项目列表(分页)
     */
    @Override
    public TableDataInfo<InfusionItemVo> queryPageList(InfusionItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InfusionItem> lqw = buildQueryWrapper(bo);
        Page<InfusionItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询输液室登记项目列表(不分页)
     */
    @Override
    public List<InfusionItemVo> queryList(InfusionItemBo bo) {
        LambdaQueryWrapper<InfusionItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InfusionItem> buildQueryWrapper(InfusionItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InfusionItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterId()), InfusionItem::getRegisterId, bo.getRegisterId());
        lqw.eq(bo.getGroupNumber() != null, InfusionItem::getGroupNumber, bo.getGroupNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), InfusionItem::getItemId, bo.getItemId());
        lqw.eq(bo.getInfusionNumber() != null, InfusionItem::getInfusionNumber, bo.getInfusionNumber());
        lqw.eq(bo.getInfusionDay() != null, InfusionItem::getInfusionDay, bo.getInfusionDay());
        lqw.eq(StringUtils.isNotBlank(bo.getLabelCode()), InfusionItem::getLabelCode, bo.getLabelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinNurse()), InfusionItem::getSkinNurse, bo.getSkinNurse());
        lqw.eq(bo.getSkinTime() != null, InfusionItem::getSkinTime, bo.getSkinTime());
        lqw.eq(bo.getSort() != null, InfusionItem::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), InfusionItem::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), InfusionItem::getRemarks, bo.getRemarks());
        lqw.like(StringUtils.isNotBlank(bo.getSkinNurseName()), InfusionItem::getSkinNurseName, bo.getSkinNurseName());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), InfusionItem::getOrgName, bo.getOrgName());
        return lqw;
    }

    /**
     * 新增输液室登记项目
     */
    @Override
    public Boolean insertByBo(InfusionItemBo bo) {
        InfusionItem add = BeanUtil.toBean(bo, InfusionItem.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInfusionItemId(add.getInfusionItemId());
        }
        return flag;
    }

    /**
     * 修改输液室登记项目
     */
    @Override
    public Boolean updateByBo(InfusionItemBo bo) {
        InfusionItem update = BeanUtil.toBean(bo, InfusionItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InfusionItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除输液室登记项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
